package com.hnkywl.sts.dao.organ;

import com.hnkywl.sts.entity.organ.Qyjg;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: lenovo
 * Date: 13-6-5
 * Time: 上午10:53
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class JhdwDao extends SimpleHibernateTemplate<Qyjg, Long> {
    public Page<Qyjg> findJhdwList(String qc,String jc,String lxr,int pageNo, int pageSize) {
        String hql = null;
        String lxrHql = "";
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Qyjg> page = new Page<Qyjg>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (qc != null && !qc.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" qc like ? ");
            param.add("%" + qc.trim() + "%");
        }
        if (jc != null && !jc.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" jc like ? ");
            param.add("%" + jc.trim() + "%");
        }
        if (lxr != null && !lxr.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" lxr like ? ");
            param.add("%" + lxr.trim() + "%");
        }
        if (condition.length() != 0)
            condition.append(" and ");
        condition.append(" type = ? ");
        param.add("qyjg");
        //==============记录数量===============
        hql = "select count(*) from Qyjg ";
        if (condition.length() != 0)
            hql = hql + " where " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Qyjg ";
        if (condition.length() != 0)
            hql = hql + " where " + condition + "order by dateCreated desc";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }
    //校验
    public Boolean checkUnique(Long id, Object propertyValue,String propertyName) {
        String hql;
        if (propertyValue == null)
            return false;
        if(id == null){
            //修改检验
            hql = "from Qyjg where " + propertyName + " = ? ";
            return this.existsQuery(hql, propertyValue);
        }else{
            //新增检验
            hql = "from Qyjg where " + propertyName + " = ? and id <> ?";
            return this.existsQuery(hql, propertyValue,id);
        }
    }
}
